//
//  LoginVC.swift
//  easyVideo-swift
//
//  Created by quanhao huang on 2021/7/29.
//

import UIKit

class LoginVC: UIViewController, UITextFieldDelegate {
    
    @IBOutlet weak var serverTF: UITextField!
    @IBOutlet weak var accoutTF: UITextField!
    @IBOutlet weak var passwordTF: UITextField!
    @IBOutlet weak var loginButton: UIButton!
    @IBOutlet weak var adviceButton: UIButton!
    
    //页面将要显示
    override func viewWillAppear(_ animated: Bool) {
        
    }
    //页面已经显示
    override func viewDidAppear(_ animated: Bool) {
        
    }
    //页面已经准备好
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //设置默认的输入内容
        serverTF.text = "172.12.0.0"
        accoutTF.text = "limiaomiao"
        passwordTF.text = "123456"

        passwordTF.delegate = self
        //添加按钮方法
        loginButton.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        //圆角
        loginButton.layer.cornerRadius = 4.0
        //设置文字
        adviceButton.setTitle("高级设置", for: .normal)
    }
    //页面将要销毁
    override func viewWillDisappear(_ animated: Bool) {
        
    }
    //页面已经销毁
    override func viewDidDisappear(_ animated: Bool) {
        
    }

    @IBAction func backAction(_ sender: Any) {
        //返回上一个界面
        self.navigationController?.popViewController(animated: true)
    }
    
    //登录方法
    @objc func loginAction() -> Void {
        print("点击了登录")
        /*
        //字符串是空字符串，所以count的数量 = 0
        serverTF.text = ""
        //text没有值为nil， 所以他的count是nil,即为0
        serverTF.text = nil
        */
        if serverTF.text?.count == 0 {
            print("")
        }else if accoutTF.text?.count == 0 {
            print("")
        }else if passwordTF.text?.count == 0 {
            
        }else if passwordTF.text?.count ?? 0 < 6 {//如果text为nil那就把count默认为0
            print("密码输入小于6位了")
        }
        
        //登录(拿到server accout password 然后向服务器请求， post)服务器返回结果，来做判读
        
    }
    
    //实现代理方法
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        //循环判断
        switch textField {
        case serverTF:
            //server 不允许输入超过6个数 0 1 2 3 4 5 (6>5 return)
            if serverTF.text!.count > 5 {
                //提示框
                let alert = UIAlertController.init(title: "提示", message: "不允许输入超过6个数", preferredStyle: .alert)
                present(alert, animated: true, completion: nil)
            }
            break
        case accoutTF:
            //accoutTF 不允许超过10个数
            break
        case passwordTF:
            //password 不允许超过20个数
            if passwordTF.text!.count > 20 {
                return false
            }
            break
        default:
            break
        }
        return true
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
